Ext.define('MyApp.controller.quartzCron.QuartzCronController', {
    extend: 'MyApp.controller.share.BaseGridController',
    views : ['quartzCron.QuartzCronGrid','quartzCron.QuartzCronForm'],
	stores:['QuartzCronStore'],
	models:['QuartzEntity'],
	requires: [
            'Ext.ux.ProgressBarPager'
    ],
    refs: [
           {ref: 'quartzCronView',selector:'quartzCron-grid'},
           {ref:'quartzCronForm',selector:'quartzCronForm'}
    ],
	init: function() {
		this.control({
			//为选中时按钮不可用
			'quartzCron-grid': {
				select: function(){
					this.onGridSelect(this.getQuartzCronView());
				},
				deselect: function(){
					this.onGridDSelect(this.getQuartzCronView());
				}
			},
	        'quartzCron-grid button#add' : {
	        	click:function(btn){
					this.onAddButtonClick('quartzCronForm');
				}
	        },
	        'quartzCron-grid button#start' : {
	        	click:'onStartButtonClick'
	        },
	        'quartzCron-grid button#pause' : {
	        	click:'onPauseButtonClick'
	        },
	        'quartzCron-grid button#stop' : {
	        	click:'onStopButtonClick'
	        },
	        'quartzCron-grid button#edit' : {
	        	click:function(btn){
	        		var record = btn.up('quartzCron-grid').getSelectionModel().getLastSelected();
					if("停止"==record.get('status')){
						this.onEditButtonClick(btn,'quartzCron-grid','quartzCronForm');
					}else{
				   		Ext.MessageBox.alert('提示', '任务未停止,请先停止任务后再修改!');
				   	}
	        	}
	        },
	        'quartzCron-grid button#delete' : {
	        	click:function(btn){
					  var record = btn.up('quartzCron-grid').getSelectionModel().getLastSelected();
					  if("停止"==record.get('status')){
					  		this.onDeleteButtonClick(btn,'quartzCron-grid');
					  }else{
					   		Ext.MessageBox.alert('提示', '任务未停止,请先停止任务后再删除!');
					  }
	        	}
	        },
	        'quartzCronForm button#save' : {
	        	click:function(btn){
	        		var me = this;
	        		me.onSaveButtonClick(btn,me.getQuartzCronStoreStore(),me.getQuartzEntityModel());
	        	}
	        },
	        'quartzCronForm button#cancel' : {
	        	click:'onCancelButtonClick'
	        },
	        'quartzCronForm textfield#Cron' : {
	        	change:function(tf){
	        		this.getQuartzCronForm().down('form').getForm().findField('cronExpression').setValue(tf.getValue());
	        	}
	        }
		});
	},
	onStartButtonClick:function(btn){
		var record = btn.up('quartzCron-grid').getSelectionModel().getLastSelected();
		Ext.Ajax.request({
		    url: 'quartzCron/startQuartzCron',
		    jsonData: Ext.encode(record.getData()),
		    success: function(response){
		        var text = response.responseText;
		        if(text==1){
					record.set('status','启动');
					record.commit(); 
		        	Ext.MessageBoxUtil.msg('提示','启动成功!');
		        }else{
		        	Ext.MessageBoxUtil.msg('提示','启动失败!');
		        }
		    },
             failure: function (response, options) {
             	Ext.MessageBoxUtil.msg('提示','启动失败,错误编号：'+ response.status);
             }
		});
	},
	onStopButtonClick:function(btn){
		var record = btn.up('quartzCron-grid').getSelectionModel().getLastSelected();
		Ext.Ajax.request({
		    url: 'quartzCron/stopQuartzCron',
		    jsonData: Ext.encode(record.getData()),
		    success: function(response){
		        var text = response.responseText;
		        if(text==1){
		        	//record.beginEdit();
					record.set('status','停止');
					//record.endEdit();
					record.commit(); 
		        	Ext.MessageBoxUtil.msg('提示','停止成功!');
		        }else{
		        	Ext.MessageBoxUtil.msg('提示','停止失败!');
		        }
		    }
		});
	},
	onPauseButtonClick:function(btn){
		var record = btn.up('quartzCron-grid').getSelectionModel().getLastSelected();
		Ext.Ajax.request({
		    url: 'quartzCron/pauseQuartzCron',
		    jsonData: Ext.encode(record.getData()),
		    success: function(response){
		        var text = response.responseText;
		        if(text==1){
		        	//record.beginEdit();
					record.set('status','暂停');
					//record.endEdit();
					record.commit(); 
		        	Ext.MessageBoxUtil.msg('提示','暂停成功!');
		        }else{
		        	Ext.MessageBoxUtil.msg('提示','暂停失败!');
		        }
		    }
		});
	}
});